home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / HyperCard Related / XCMDs & XFCNs / StackToPICSFile 1.1 / PICSFileRoutines.p next >
Encoding:
Text File  |  1991-04-05  |  5.2 KB  |  225 lines  |  [TEXT/MPS ]

  1. {$R-}
  2. {$S PICSFileRoutines }
  3.  
  4.     PICSFileRoutines —— Pascal routines that can be used to create a PICS file
  5.     
  6.     These routines were initially written to support the development of the
  7.     Hypercard XFCN StackToPICSFile, but could be of general utility for any program
  8.     that wishes to create PICS files.
  9.     
  10.     All of the routines return a boolean indicating success.  If the return
  11.     value is false, then an explanatory message will have been placed into the
  12.     VAR parameter ErrorMessage.
  13.  
  14. }
  15.  
  16. UNIT PICSFileRoutines;
  17.  
  18. INTERFACE
  19.  
  20. USES 
  21.         Resources, Packages,
  22.         OSIntf,  QuickDraw;
  23.  
  24. TYPE
  25.  
  26. FrameProcPtr = ProcPtr; { FUNCTION FrameProcPtr(CardNum: Integer; 
  27.                                                 VAR theImage: PicHandle; 
  28.                                                 VAR ErrorMessage: Str255): Boolean; }
  29.                                                 
  30.                                                 
  31. FUNCTION StartMakingPICSFile(FileName: Str255; 
  32.                                 CreatorType: OSType;
  33.                                 VAR resRefNum: Integer; 
  34.                                 VAR ErrorMessage: Str255): Boolean;
  35.  
  36. FUNCTION AddFramesToPICSFile(resRefNum: Integer; 
  37.                                 NumFrames: Integer; 
  38.                                 {FrameProcPtr}
  39.                                 FUNCTION FrameProc(CardNum: Integer; 
  40.                                                 VAR theImage: PicHandle; 
  41.                                                 VAR ErrorMessage: Str255): Boolean;
  42.                                 VAR ErrorMessage: Str255): Boolean;
  43.  
  44. FUNCTION FinishMakingPICSFile(VAR resRefNum: Integer; VAR ErrorMessage: Str255): Boolean;
  45.         
  46.  
  47. IMPLEMENTATION
  48.  
  49. CONST
  50.  
  51. PICSResourceBase =         128;    {* First resource ID in a PICS file *}
  52.  
  53. FUNCTION AddPICTToPICSFile(ThePict: PicHandle; resourceNum: Integer; 
  54.             resRefNum: Integer; VAR ErrorMessage: Str255): Boolean;
  55. VAR
  56.     Err:                    Integer;
  57.     DebugString:            Str255;
  58. BEGIN
  59.     AddPICTToPICSFile := FALSE;
  60.     
  61.     UseResFile(resRefNum);
  62.     Err := ResError;
  63.     IF (Err <> noErr) THEN
  64.     BEGIN
  65.         ErrorMessage := 'Couldn’t start using the specified resource file';
  66.         Exit(AddPICTToPICSFile);
  67.     END;
  68.  
  69.     AddResource(Handle(ThePict), 'PICT', resourceNum, 'Frame of PICS Sequence');
  70.     Err := ResError;
  71.     IF (Err <> noErr) THEN
  72.     BEGIN
  73.         NumToString(Err, DebugString);
  74.         ErrorMessage := concat('Couldn’t add PICT resource to resource file: ', DebugString);
  75.         Exit(AddPICTToPICSFile);
  76.     END;
  77.                 
  78.     UpdateResFile(resRefNum);
  79.     Err := ResError;
  80.     IF (Err <> noErr) THEN
  81.     BEGIN
  82.         ErrorMessage := 'Couldn’t update resource file after adding resource';
  83.         Exit(AddPICTToPICSFile);
  84.     END;
  85.     
  86.  
  87.     {* SetResAttrs(Handle(ThePict), resPurgeable); *}
  88.     
  89.     {* We detach the handle so that it does not continue to be a resource
  90.         handle.  It wasn’t one on the way in; we don’t want it to be one on
  91.         the way out.
  92.         *}
  93.     DetachResource(Handle(ThePict));
  94.     
  95.     AddPICTToPICSFile := TRUE;
  96. END;
  97.  
  98. FUNCTION StartMakingPICSFile(FileName: Str255; 
  99.                                 CreatorType: OSType;
  100.                                 VAR resRefNum: Integer; 
  101.                                 VAR ErrorMessage: Str255): Boolean;
  102. VAR
  103.     Err:            Integer;
  104.     info:            FInfo;
  105.  
  106. BEGIN
  107.     StartMakingPICSFile := FALSE;
  108.     
  109.     CreateResFile(FileName);
  110.     IF (ResError <> NoErr)  THEN
  111.     BEGIN
  112.         ErrorMessage := concat('Couldn’t create file: ', FileName, '.  It may already exist');
  113.         Exit(StartMakingPICSFile);
  114.     END;
  115.     
  116.     resRefNum := OpenResFile(FileName);
  117.     IF (ResError <> NoErr) THEN
  118.     BEGIN
  119.         ErrorMessage := concat('Created a PICS file, but then couldn’t open it: ', FileName);
  120.         Exit(StartMakingPICSFile);
  121.     END;  
  122.     
  123.     Err := GetFInfo(FileName, 0, info);
  124.     IF (Err <> NoErr) THEN
  125.     BEGIN
  126.         CloseResFile(resRefNum);
  127.         ErrorMessage := concat('Could not get file info for file: ', FileName);
  128.         Exit(StartMakingPICSFile);
  129.     END;  
  130.     
  131.     info.fdType := 'PICS';
  132.     info.fdCreator := CreatorType;
  133.  
  134.     Err := SetFInfo(FileName, 0, info);
  135.     IF (Err <> NoErr) THEN
  136.     BEGIN
  137.         CloseResFile(resRefNum);
  138.         ErrorMessage := concat('Could not set file info for file: ', FileName);
  139.         Exit(StartMakingPICSFile);
  140.     END;  
  141.         
  142.     StartMakingPICSFile := TRUE;
  143.  
  144. END;
  145.  
  146. FUNCTION AddFramesToPICSFile(resRefNum: Integer; 
  147.                                 NumFrames: Integer; 
  148.                                 FUNCTION FrameProc(CardNum: Integer; 
  149.                                                 VAR theImage: PicHandle; 
  150.                                                 VAR ErrorMessage: Str255): Boolean;
  151.                                 VAR ErrorMessage: Str255): Boolean;
  152. VAR
  153.     FrameNum:             Integer;
  154.     theFrame:            PicHandle;
  155.     Success:            Boolean;
  156.     resourceNum:        Integer;
  157.  
  158. BEGIN
  159.     AddFramesToPICSFile := FALSE;
  160.     Success := TRUE;
  161.     
  162.     FOR FrameNum := 1 to NumFrames DO
  163.     BEGIN
  164.     
  165.         theFrame := PicHandle(NewHandle(0));
  166.         IF (theFrame = NIL) THEN 
  167.         BEGIN
  168.             Success := FALSE;
  169.             ErrorMessage := 'Couldn’t allocate zero-size handle';
  170.             Leave;
  171.         END;
  172.         
  173.         IF (FrameProc(FrameNum, theFrame, ErrorMessage) = FALSE) THEN 
  174.         BEGIN
  175.             Success := FALSE;
  176.             {* ErrorMessage was set by calling FrameProc *}
  177.             DisposHandle(Handle(theFrame));
  178.             Leave;
  179.         END;
  180.         
  181.         resourceNum := PICSResourceBase + FrameNum - 1;
  182.         Success := Success AND AddPICTToPICSFile(theFrame, resourceNum, resRefNum, ErrorMessage);
  183.         IF (NOT Success) THEN 
  184.         BEGIN
  185.             {* ErrorMessage was set by AddPICTToPICSFile *}
  186.             DisposHandle(Handle(theFrame));
  187.             Leave;
  188.         END;
  189.         
  190.         DisposHandle(Handle(theFrame));
  191.         
  192.     END;
  193.  
  194.     AddFramesToPICSFile := Success;
  195.  
  196.     IF (NOT Success) THEN 
  197.     BEGIN
  198.         {* ErrorMessage was set during the loop above. *}
  199.         Exit(AddFramesToPICSFile);
  200.     END;
  201. END;
  202.  
  203. FUNCTION FinishMakingPICSFile(VAR resRefNum: Integer; VAR ErrorMessage: Str255): Boolean;
  204. VAR
  205.     Err:            Integer;
  206. BEGIN
  207.     FinishMakingPICSFile := FALSE;
  208.     
  209.     CloseResFile(resRefNum);
  210.     Err := ResError;
  211.     IF (Err <> NoErr) THEN 
  212.     BEGIN
  213.         ErrorMessage := concat('Error closing file');
  214.         Exit(FinishMakingPICSFile);
  215.     END;
  216.  
  217.     FinishMakingPICSFile := TRUE;
  218.  
  219. END;
  220.         
  221. END. { PICSFileRoutines Unit }
  222.  
  223.  
  224.